package com.homegoods.videomodule.ui.activity;

import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewTreeObserver;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.danikula.videocache.HttpProxyCacheServer;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.classify.model.Constant;
import com.homegoods.resource.HomeGoodsApp;
import com.homegoods.videomodule.R;
import com.homegoods.videomodule.databinding.VideoActivityShortVideoBinding;
import com.homegoods.videomodule.model.FreedomCommunityModel;
import com.homegoods.videomodule.presenter.ShortVideoPresenter;
import com.homegoods.videomodule.ui.fragments.CommentListDialogFragment;
import com.homegoods.videomodule.ui.view.ShortVideo;
import com.homegoods.videomodule.ui.view.ShortVideoView;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.util.GlideApp;
import com.kedll.library.util.MyUtils;
import com.share.sharelibrary.share.ShareData;

import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;

/**
 * 视频播放
 *
 * @author QiQi
 */
@Route(path = ARouterPath.PATH_SHOT_VIDEO)
public class ShortVideoActivity extends MyBaseFragmentActivity<VideoActivityShortVideoBinding, ShortVideoPresenter> implements ShortVideoView {

    @Autowired(name = "data")
    public FreedomCommunityModel model;

    private ShortVideo jzvdStd;

    @Override
    public int getLayout() {
        return R.layout.video_activity_short_video;
    }

    @Override
    protected void initEvent() {
        mPresenter.loadData();

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        ARouter.getInstance().inject(this);

        jzvdStd = mBinding.videoPlayer;

        mPresenter.setShortVideoInfo(jzvdStd, model);
        jzvdStd.setOnShortVideoListener(mPresenter);

        loadThumb();

        HttpProxyCacheServer proxy = getProxy();
        String url = proxy.getProxyUrl(Constant.SERVER_URL + model.getPlayUrl(), false);
        Jzvd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_PARENT);

        jzvdStd.setUp(new JZDataSource(url), Jzvd.SCREEN_WINDOW_NORMAL);
        jzvdStd.directPlay();
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        appBarLayout.setBackgroundResource(R.color.transparent);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white);
    }

    @Override
    protected ShortVideoPresenter getPresenter() {
        return new ShortVideoPresenter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Jzvd.goOnPlayOnResume();
    }

    @Override
    protected void onDestroy() {
        mPresenter.setShortVideoInfo(null, null);
        jzvdStd.setOnShortVideoListener(null);
        super.onDestroy();
    }

    private void loadThumb() {
        supportPostponeEnterTransition();

        GlideApp.with(this)
                .asBitmap()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .load(Constant.SERVER_URL + model.getSmallPic())
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        jzvdStd.thumbImageView.setImageBitmap(resource);
                        scheduleStartPostponedTransition(jzvdStd.thumbImageView);
                    }
                });

    }

    public HttpProxyCacheServer getProxy() {
        return HomeGoodsApp.getProxy(this);
    }

    private void scheduleStartPostponedTransition(final View shareElement) {
        shareElement.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                shareElement.getViewTreeObserver().removeOnPreDrawListener(this);
                supportStartPostponedEnterTransition();
                return true;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        MyUtils.setSystemUi(getWindow(), false);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Jzvd.clearSavedProgress(this, null);
        Jzvd.goOnPlayOnPause();
        if (isFinishing()) {
            MyUtils.setSystemUi(getWindow(), true);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        Jzvd.releaseAllVideos();
        Jzvd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_ADAPTER);
        super.onBackPressed();
        supportFinishAfterTransition();
    }

    @Override
    public void onForward() {
        new ShareData.Builder()
                .setShareType(ShareData.SHARE_VIDEO)
                .setUrl(Constant.SERVER_URL + model.getPlayUrl())
                .setThumbUrl(Constant.SERVER_URL + model.getSmallPic())
                .builder()
                .startShare();
    }

    @Override
    public void showComment() {
        CommentListDialogFragment dialogFragment = new CommentListDialogFragment();
        dialogFragment.setData(model);
        dialogFragment.showDialog(getSupportFragmentManager(), "commentDialog");
    }
}
